<?php 	namespace Admin\Controller;
class CategoryController extends CommonController
{
	protected $db;
	public function __init()
	{
		$this->db = new \Common\Model\Category;
	}
	public function index()
	{
		$data=$this->db->orderBy("category_sort","ASC")->get();
		$data= Data::tree($data,"category_name",'category_id','category_pid');
//		p($data);
		View::with("data",$data);
		View::make();
		
	}
	public function add()
	{
		
		$type = Db::table("type")->get();
		View::with("type",$type);
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				View::error($this->db->getError());
			}else{
				View::success("添加成功",U('index'));
			}
//			p($_POST);die;
		}
		View::make();
		
	}
	public function addSon()
	{
		$category_id = Q("get.category_id",0,"intval");
		$type = Db::table("type")->get();
		View::with("type",$type);
		$category = $this->db->where("category_id",$category_id)->get();
		View::with("category",$category);
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				View::error($this->db->getError());
			}else{
				View::success("添加成功",U('index'));
			}
		}
		View::make();
	}
	public function edit()
	{
		
		
		
		
		$category_id = Q("get.category_id",0,"intval");
		$type = Db::table("type")->get();
		
		View::with("type",$type);
		$oldData = $this->db->where("category_id",$category_id)->first();
		
		View::with("oldData",$oldData);
		$cateData = $this->db->getCataData($category_id);
		
		//分配数据
		View::with('cateData',$cateData);
		if(IS_POST)
		{
			if(!$this->db->edit())
			{
				View::error($this->db->getError());
			}else{
				View::success("修改成功",U('index'));
			}
		}
		View::make();
	}
	public function del()
	{
		$category_id = Q("get.category_id",0,"intval");
		//执行删除
		$this->db->del($category_id);
		//成功跳转
		$this->success("删除成功",U("index"));
	}
}
